package com.x.utils.http;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.x.utils.StringUtil;

/**
 * 用于组装 http get参数 如a=b&b=c ,经过urlencode
 * 
 * 不需要依赖第三方库
 * 
 * @author Hugo
 *
 */
public class ParamUtil {

	private static Logger log = Logger.getLogger(ParamUtil.class);

	/**
	 * 按升序重新排序参数，主要用于签名使用
	 * 
	 * @param request
	 * @param and
	 *            连接标识符，如为null，则默认&
	 * @param filters
	 *            不参与排序的参数名称
	 * @return 返回排序后的值
	 */
	public static String getSortRequestValues(HttpServletRequest request, String and, String... filters) {
		/*************************************** 组装重排序参数 *********************************************/
		and = StringUtil.isEmpty(and) ? "&" : and;
		Map<String, String> map = new HashMap<>();
		@SuppressWarnings("unchecked")
		Enumeration<String> attributeNames = request.getParameterNames();
		while (attributeNames.hasMoreElements()) {
			String name = attributeNames.nextElement();
			map.put(name, request.getParameter(name));
		}

		StringBuilder contentBuffer = new StringBuilder();
		Object[] signParamArray = map.keySet().toArray();
		Arrays.sort(signParamArray);

		for (Object key : signParamArray) {
			String value = map.get(key);
			if (!isFilter(key, filters)) {// 判断是否不参与排序
				contentBuffer.append(value + and);
			}
		}
		return contentBuffer.toString();
	}

	private static boolean isFilter(Object key, String... filters) {
		if (filters == null) {
			return false;
		}
		for (String f : filters) {
			if (f.equals(key)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * params转MAP工具
	 * params格式为：commandId=20003&gameId=10010&sn=0&version=0&adultFlag=0&
	 * errorMessage=帐号密码错误&code=0001&customerId=
	 * 
	 * @param params
	 * @return
	 */
	public static Map<String, String> queryStringToMap(String params) {
		String[] rs = params.split("&");
		Map<String, String> map = new HashMap<String, String>(2);
		for (String ss : rs) {
			String[] s1 = ss.split("=");
			if (s1.length < 2) {
				map.put(s1[0], "");
			} else {
				map.put(s1[0], s1[1]);
			}
		}
		return map;
	}

	/**
	 * 打印接收到的Post值
	 * 
	 * @param request
	 */
	public static void PrintParams(HttpServletRequest request, String name) {
		@SuppressWarnings("unchecked")
		Enumeration<String> e = request.getParameterNames();
		String paramName, paramValue;
		StringBuilder sb = new StringBuilder();
		sb.append(name + " recevie data>>>");
		while (e.hasMoreElements()) {
			paramName = e.nextElement();
			paramValue = request.getParameter(paramName);
			sb.append(paramName).append("=").append(paramValue).append("&");
		}
		log.info(sb.toString());
	}

	public static void main(String[] args) {
		Map<String, String> map = queryStringToMap("a=a1&b=b1&c=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87&d=1000");

		String and = "#";
		String[] filters = null;

		StringBuilder contentBuffer = new StringBuilder();
		Object[] signParamArray = map.keySet().toArray();
		Arrays.sort(signParamArray);

		and = StringUtil.isEmpty(and) ? "&" : and;
		for (Object key : signParamArray) {
			String value = map.get(key);
			if (!isFilter(key, filters)) {// 判断是否不参与排序
				contentBuffer.append(value + and);
			}
		}
		System.out.println(contentBuffer.toString());
	}

}
